
from yacf.Frontend.C99 import c99_lexer 
from ply.lex import TOKEN
class AccLexer(object):


   
    @staticmethod
    def get_reserved():
        reserved = {
                'pragma'    : 'PRAGMA',
                'name'		: 'NAME', # ULL Addon to Acc 
                # acc v1.0 syntax
                'acc'   	: 'ACC',
                'host'      : 'HOST',
                'device'    : 'DEVICE',
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #           Region clauses 
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                'parallel'	: 'PARALLEL',
                'kernels'	: 'KERNELS',
                'data'      : 'DATA',
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #           Loop  clauses 
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                'loop'      : 'LOOP',
                'collapse'	: 'COLLAPSE',
                'num_gangs'    : 'NUM_GANGS',
                'gang'        : 'GANG',
                'num_workers'      : 'NUM_WORKERS',
                'worker'        : 'WORKER',
                'vector_length'    : 'VECTOR_LENGTH',
                'vector'    : 'VECTOR',
		'independent' : 'INDEPENDENT',
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #           Data movement clauses 
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                      'copyin'	   : 'COPYIN',
                       'copyout'	: 'COPYOUT',
                       'copy'	   : 'COPY',
                      'create'    : 'CREATE',
                      'private'	: 'PRIVATE',
                      'firstprivate': 'FIRSTPRIVATE',
                      'pcopyin' : 'PCOPYIN', 
                      # 'present_or_copyin'	   : 'PCOPYIN',
                      'pcopyout'	: 'PCOPYOUT',
                      'pcopy'	   : 'PCOPY',
                      'pcreate'    : 'PCREATE',
                      'host_data'   : 'HOSTDATA',
                      'use_device'  : 'USEDEVICE',
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #            Syncronization
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                'reduction'	: 'REDUCTION',
                'async'     : 'ASYNC',
                'wait'	        : 'WAIT',
                'update'	: 'UPDATE',
            }
        return reserved


  
    # tokens = list(c99_lexer.C99Lexer.tokens) + [] + list(reserved.values())

    @staticmethod
    def get_tokens():
            #allreserved = reserved.copy()
            #allreserved.update(c99_lexer.C99Lexer.keyword_map)
            tokens = list(AccLexer.get_reserved().values()) + ['ACCIGNORE'] #['ACCIGNORE', 'CONTENTIGNORE', 'ENDIGNORE']
            return tokens

    def t_ACCIGNORE(self, t):
        r'_yacf_ignore_\n[^$]+_yacf_ignore_'
        print 'En comentario ignore acc'
        #t.type = 'ACCIGNORE'
        return t

#    def t_ANY_ACCIGNORE(self, t):
#        r'foo'
#        print 'Estoy en ignoreeeeee'
#        t.lexer.begin('ACCIGNORE')
#        return t
#
#    def t_ACCIGNORE_ENDIGNORE(self, t):
#        r'foo'
#        t.lexer.begin('INITIAL')
#        return t
#
#    def t_ACCIGNORE_CONTENTIGNORE(self, t):
#        r'[^$]+\#pragma end'
#        print 'Contenido del estado acc ignore'
#        return t

    @staticmethod
    def get_states():
#        states = (
#		    ('ACCIGNORE', 'exclusive'),
#	      )
        states = tuple()
        return states

    def t_directive_ID(self, t):
        r'[a-zA-Z_][0-9a-zA-Z_]*'
        t.type = AccLexer.get_reserved().get(t.value, 'ID') 
        return t
#

if __name__ == '__main__':
    filename = '../testsC99/5_omp.c'
    text = open(filename).read()
    
    def errfoo(msg, a, b):
        printme(msg)
        sys.exit()
    
    def typelookup(namd):
        return False
    
    omplex = OmpLexer(errfoo, typelookup)
    omplex.build()
    omplex.input(text)
    
    while 1:
        tok = omplex.token()
        if not tok: 
            break
        print tok
